<?php

/**
 * This is the model class for table "artist_project".
 *
 * The followings are the available columns in table 'artist_project':
 * @property string $id
 * @property string $title
 * @property string $image
 * @property string $description
 * @property integer $artist
 */
class ArtistProject extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return ArtistProject the static model class
     */
    public $image_file = 'aaa';
    public $image_file_1;
    public $image_file_2;
    public $image_file_3;
    public $image_file_4;
    public $image_file_5;
    public $show_home;
	public $home_show;
	public $type = 'project';
    
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'artist_project';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('artist,title,home_show', 'required'),
            array('artist,show_home', 'numerical', 'integerOnly' => true),
            array('title, tag, image', 'length', 'max' => 255),
            array('image_file_1,image_file_2,image_file_3,image_file_4,image_file_5', 'file', 'allowEmpty' => true,
                'types' => 'jpg, gif, png, jpeg',
                'maxSize' => 1024 * 1024 * 10, // 10MB
                'tooLarge' => 'The file was larger than 10MB. Please upload a smaller file.',
            ),
            array('description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, tag, image, description, artist', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'artist_name' => array(self::BELONGS_TO, 'Artist', 'artist'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => 'Title',
            'image' => 'Image',
            'image_file' => 'Image File 1',
            'description' => 'Description',
            'artist' => 'Artist',
            'show_home' => 'Homepage Position',
			'home_show' => 'SHow On Front Page'
        );
    }


    public function beforeDelete() {
        $images = ProjectImage::model()->findAllByAttributes(array('project'=>$this->id));
        foreach($images as $image){
            $image_model = ProjectImage::model()->findByPk($image->id);
            $image_model->delete();
        }
        return true;
    }

    public function afterSave() {
        if($this->show_home != null)
        {
            $home_model = Home::model()->findByPk($this->show_home);
            $home_model->type = 'project';
            $home_model->owner = $this->id;
            $home_model->save();
        }
        $this->addSearch();
        $this->uploadImage();
    }

    public function addSearch() {
        $search_model = SearchAll::model()->findByAttributes(array('type' => 'project', 'real_id' => $this->id));
        if ($search_model) {
            $search_model->short = $this->title;
            $search_model->save();
        }else{
            $search_model = new SearchAll;
            $search_model->short = $this->title;
            $search_model->type = 'project';
            $search_model->real_id = $this->id;
            $search_model->save();
        }
    }
    public function uploadImage() {
        $model = $this->model();
        $images = array('image_file_1','image_file_2','image_file_3','image_file_4','image_file_5');
        foreach ($images as $value) {
            $image = CUploadedFile::getInstance($model, $value);
            if ($image != null) {
                $image_model = new ProjectImage;
                $image_model->image_obj = $image;
                $image_model->project = $this->id;
                $image_model->save();
            }
        }
    }
    public function getArtist() {
        $artist = Artist::model()->findByPk($this->artist);
        return $artist->name;
    }
    
    public function getThumbnail() {
        $image_model = ProjectImage::model()->findByAttributes(array('project'=>$this->id));
        if($image_model){
            $image_parts = pathinfo($image_model->image);
            return Yii::app()->baseUrl . '/' . $image_parts["dirname"] . '/' . $image_parts["filename"] . '_thumb.' . $image_parts["extension"];
        }else{
             return false;
        }
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('image', $this->image, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('artist', $this->artist);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}